package com.hy.chat.client;


import com.extjs.gxt.ui.client.GXT;
import com.extjs.gxt.ui.client.data.BaseLoader;
import com.extjs.gxt.ui.client.data.BaseModel;
import com.extjs.gxt.ui.client.data.DataProxy;
import com.extjs.gxt.ui.client.data.Loader;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.HtmlEditor;
import com.extjs.gxt.ui.client.widget.layout.FitData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.hy.chat.client.widget.ChatWindow;



public class AdminChat implements EntryPoint {
	
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	
	private final ChatServiceAsync greetingService = GWT
			.create(ChatService.class);


	public void onModuleLoad() {
				
		if (RootPanel.get("adminChat")==null) return;
		System.out.println("********* this is AdminChat Entry point .");
		ChatWindow window = new ChatWindow("aaaaaaaaaaa在线服务");
		
			
			ContentPanel contentPanel = new ContentPanel();
			contentPanel.setHeaderVisible(false);
			
				final TabPanel folder = new TabPanel();  
			    folder.setWidth(450);  
			    folder.setHeight(100);  
			    folder.setAutoHeight(true);  
			  
			    final TabItem shortText = new TabItem("Short Text");  
			    shortText.addStyleName("pad-text");  
			    shortText.setHeight(100);  
			    //chatTabItem.setData("messageIndex", 0);
			    final Html html = shortText.addText("00000000000<br>");  
			    String string = html.getHtml();
			    html.setHtml(string+"2222222222222");
			    
			    folder.add(shortText);  
			    		    	    
				
			contentPanel.add(folder);
			final HtmlEditor htmlEditor = new HtmlEditor();  
			htmlEditor.setSize(200, 100);
			htmlEditor.setHideLabel(true);  
		    contentPanel.add(htmlEditor, new FormData("100% -53"));  
		    
		    	final Button button = new Button("发送");
		    	button.setData("messageIndex", 0);
		    	//为按钮，用于完成：1提交和返回数据2清空文本编辑器
		    	button.addSelectionListener(new SelectionListener<ButtonEvent>() {
					
					@Override
					public void componentSelected(ButtonEvent ce) {
						
						MessageBox.alert("提示",	"发送成功！", null);

						BaseModel indexAndMessage = new BaseModel();
						indexAndMessage.set("index", button.getData("messageIndex"));
						indexAndMessage.set("message", htmlEditor.getValue());
						
						
						greetingService.getIndexAndMessage(indexAndMessage,  new AsyncCallback<BaseModel>() {

							@Override
							public void onFailure(Throwable caught) {
								System.out.println("*** "+GWT.getModuleBaseURL()+"chatService");

								MessageBox.alert("警告",	"后台的Rpc调用失败，请联系管理员或者稍后再试！", null);
							}

							@Override
							public void onSuccess(BaseModel returnBaseModel) {											
								
								htmlEditor.setValue("");
								html.setHtml(html.getHtml()+"<br>333333"+returnBaseModel.get("message"));
								button.setData("messageIndex", returnBaseModel.get("index"));

								
								shortText.repaint();
							}
						});
						
					}
				});
		    contentPanel.add(button);
		
//		window.add(folder);
		window.add(contentPanel);
		
		
		
		RootPanel.get("adminChat").add(window);

	}
}
